<?php

namespace app\console\tool\controllers;

use app\console\ConsoleController;

class PropertyController extends ConsoleController
{
    public function generate($sql): string
    {
        $str = '';
        preg_match_all('/`(.*?)` (.*?) (.*?),/u', $sql, $matchs, PREG_SET_ORDER);
        foreach ($matchs as $match) {
            $field = $match[1];
            $type = (strrpos($match[2], 'int') !== false) ? 'int' : 'string';
            preg_match_all("/COMMENT '(.*?)'/u", $match[3], $commentMatch, PREG_SET_ORDER);
            $comment = empty($commentMatch[0][1]) ? '' : $commentMatch[0][1];
            $str .= " * @property {$type} \${$field} $comment" . PHP_EOL;
        }
        $str = trim($str);
        return <<<tem
/**
{$str}
 */
tem;

    }

    public function genAdmin($sql): string
    {
        $str = '';
        preg_match_all('/`(.*?)` (.*?) (.*?),/u', $sql, $matchs, PREG_SET_ORDER);
        foreach ($matchs as $match) {
            $field = $match[1];
            $type = (strrpos($match[2], 'int') !== false) ? 'int' : 'string';
            preg_match_all("/COMMENT '(.*?)'/u", $match[3], $commentMatch, PREG_SET_ORDER);
            $comment = empty($commentMatch[0][1]) ? '' : $commentMatch[0][1];
            if (empty($comment)) {
                $comment = $field;
            }
            $str .= <<<tem
 '{$field}' => [
            'name'    => '$comment',
            'type'    => 'text',
            'order_type' => 'desc',
            'display' => '+list,+edit,-search,+detail',
        ],
tem;
            $str .= PHP_EOL;
;
        }
        return trim($str);
    }
}